<?php
namespace app\api\model;
use \think\Db;
use \think\Model;
use \app\api\service\Token;
use think\Cache;
class Article extends Model
{
    /**
     * 文章列表(随机)
     */
    public function ArtList($cate_id,$page,$listRows)
    {
        $field = [
            'a.article_id' => 'article_id',
            'a.art_title' => 'art_title',
            'a.browse_num' => 'browse_num',
            'a.art_content' => 'art_content',
            'c.cate_id' => 'cate_id',
            'c.cate_name' => 'cate_name'
        ];
        $where = [
            'a.is_del' => 0,
            'a.cate_id' => $cate_id
        ];
        if ($cate_id == 0) {
            unset($where['a.cate_id']);
        }
        //总记录数
        $count = Db::name('article')->alias("a")->where($where)->count();
        //总页数
        $totalPage = ceil($count/ $listRows);
        $list = Db::name("article")
            ->alias("a")
            ->field($field)
            ->join("__CATE__ c","a.cate_id = c.cate_id","left")
            ->orderRaw('rand()')
            ->where($where)->page($page,$listRows)->select();
        foreach ($list as $key => $value) {
            $list[$key]['face_pic'] = get_content_img($value['art_content'],3);
            unset($list[$key]['art_content']);
        }
        return [
            'list' => $list,
            'count' => $count,
            'totalPage' => $totalPage,
            'page' => $page,
            'listRows' => $listRows
        ];
    }

    /**
     * 取分类
     */
    public function selCateAll()
    {
        $where = [
            'is_del' => 0,
            'pid' => ['>',0]
        ];
        $list = Db::name('cate')->where($where)->select();
        $tmpArr[0] = [
            "cate_id"=> 0,
            "cate_name"=> "推荐",
            "pid"=> 12,
            "level"=> 2,
            "is_del"=> 0
        ];
        return [
            'list' => array_merge($tmpArr,$list)
        ];
    }

    /**
     * 取文章内容
     */
    public function getArtById($article_id)
    {
        $where = [
            'article_id' => $article_id
        ];
        $data = Db::name('article')->where($where)->find();
        return $data;
    }
}
